package com.um.jdy.app.core.configuration;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.um.jdy.app.core.properties.HshAlipayProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Slf4j
@Component
@ConditionalOnExpression("T(cn.hutool.core.util.StrUtil).isNotBlank('${hsh.alipay.app-id:}')")
public class AlipayConfigure {

    @Autowired
    HshAlipayProperties hshAlipayProperties;

    public static AlipayClient alipayClient;

    @PostConstruct
    public void initAlipayEasy() {
        Factory.setOptions(getOptions());
    }

    private Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = StrUtil.replace(hshAlipayProperties.getUrl(), "https://", "");
        config.signType = "RSA2";
        config.appId = hshAlipayProperties.getAppId();
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = hshAlipayProperties.getPrivateKey();
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
        config.merchantCertPath = hshAlipayProperties.getAppCertPath();
        config.alipayCertPath = hshAlipayProperties.getAlipayPublicCertPath();
        config.alipayRootCertPath = hshAlipayProperties.getRootCertPath();
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        // config.alipayPublicKey = "<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->";
        //可设置异步通知接收服务地址（可选）
        //config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        //config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";
        return config;
    }

    @PostConstruct
    public AlipayClient getAlipayClient() throws AlipayApiException {

        String url = hshAlipayProperties.getUrl();
        String appid = hshAlipayProperties.getAppId();
        String privateKey = hshAlipayProperties.getPrivateKey();
        String format = hshAlipayProperties.getFormat();
        String charset = hshAlipayProperties.getCharset();
        String signType = hshAlipayProperties.getSignType();
        String appCertPath = hshAlipayProperties.getAppCertPath();
        String alipayPublicCertPath = hshAlipayProperties.getAlipayPublicCertPath();
        String rootCertPath = hshAlipayProperties.getRootCertPath();

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(url);
        alipayConfig.setAppId(appid);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat(format);
        alipayConfig.setCharset(charset);
        alipayConfig.setSignType(signType);

//        alipayConfig.setAppCertPath(appCertPath);
//        alipayConfig.setAlipayPublicCertPath(alipayPublicCertPath);
//        alipayConfig.setRootCertPath(rootCertPath);

        alipayConfig.setAppCertContent(ResourceUtil.getResourceObj(appCertPath).readUtf8Str());
        alipayConfig.setAlipayPublicCertContent(ResourceUtil.getResourceObj(alipayPublicCertPath).readUtf8Str());
        alipayConfig.setRootCertContent(ResourceUtil.getResourceObj(rootCertPath).readUtf8Str());

        alipayClient = new DefaultAlipayClient(alipayConfig);
        return alipayClient;
    }


}
